using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using GSBA.ViewModels;
using Windows.Storage;
using Windows.UI.Core;

// The Split Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234234

namespace GSBA.Views
{
    /// <summary>
    /// A page that displays a group title, a list of items within the group, and details for
    /// the currently selected item.
    /// </summary>
    public sealed partial class CategoryListing : Page
    {
        public CategoryListing()
        {
            this.InitializeComponent();
            
            Window.Current.SizeChanged += OnSizeChanged;
            this.SetViewState();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            object[] parameters = e.Parameter as object[];
            this.DataContext = new CategoryListingViewModel(parameters[0] as string, parameters[1] as StorageFile);
        }

        private void SetViewState()
        {
            // get the view state after the size has changed
            string CurrentViewState = Windows.UI.ViewManagement.ApplicationView.Value.ToString();

            // using VisualStateManager make sure we navigate to the correct state definition
            VisualStateManager.GoToState(this, CurrentViewState, false);
        }
         
        /// <summary>
        /// Hide the map control on snapped views otherwise the app crashes due to a known bug in the Bing maps SDK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnSizeChanged(object sender, WindowSizeChangedEventArgs e)
        {
            switch (Windows.UI.ViewManagement.ApplicationView.Value)
            {
                case ApplicationViewState.Snapped:
                    {
                        this.ListingMap.Visibility = Visibility.Collapsed;
                        break;
                    }
                default:
                    {
                        this.ListingMap.Visibility = Visibility.Visible;
                        break;
                    }
            }

            this.SetViewState();
        } 

    }
}
